@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/{{ request()->path() }}/../" method="post" class="main-form">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('SQROLLS') }}</div>
                <div class="form-input">
                    <select name="roll_number" id="" class="form-control sq-rolls"></select>
                    <div class="text-danger residue-length mt-2" style="">{{ ts('kdhcd') }} <b>0</b> cm</div>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label">{{ ts('DHFQsd') }}</div>
                <div class="form-input">
                    <input name="ysdmc" id="" class="form-control" readonly>
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('DHFQJXS') }}</div>
                <div class="form-input">
                    <select name="ygmsid" id="" class="form-control"></select>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('SQLENGTH') }}（cm）</div>
                <div class="form-input">
                    <input type="number" name="length" value="{{ $data->roll_number ?: old('roll_number') }}" class="form-control">
                </div>
            </div>


            <div class="form-item form-s1">
                <div class="form-label">{{ ts('DHsd') }}</div>
                <div class="form-input">
                    @if(session('admin.user_type')==2)
                        <select name="xsdid" id="" class="form-control" readonly>
                            <option value="{{ session('admin.id') }}">{{ session('admin.abbr') }}</option>

                        </select>
                    @else
                        <select name="xsdid" id="" class="form-control">
                            @foreach(\App\Models\Admin::where('user_type', 2)->where('status', 1)->get() as $v)
                                <option value="{{ $v->id }}">{{ $v->abbr ?: $v->company_name }}</option>
                            @endforeach
                        </select>
                    @endif
                </div>
            </div>
            <div class="form-item form-s1">
                <div class="form-label">{{ ts('DHjxs') }}</div>
                <div class="form-input">
                    <select name="xgmsid" id="" class="form-control sq-jxs"></select>
                    {{--<div class="text-danger residue-length mt-2" style="">{{ ts('kdhcd') }} <b>0</b> cm</div>--}}
                </div>
            </div>


            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('EXPLAIN') }}</div>
            </div>
            <div class="form-item form-s1 px-4">
                <textarea name="note" id="" cols="30" rows="10" class="form-control">{{ $data->note ?: old('note') }}</textarea>
            </div>
            <div class="mt-4">
                <button type="submit" class="btn btn-info">{{ ts('SUBMIT') }}</button>
            </div>
        </form>

    </div>


@endsection

@section('script')
    <script>
        $(document).ready(function () {
            $('body').on('submit', '.main-form', function () {
                $(this).find('button[type="submit"]').attr('disabled', true);
                var that = $(this);
                var form = formObjV2('.main-form');
                var state = true;
                if (state && !form.roll_number) {
                    toastr.error('申请卷号不能为空！');
                    state = false;
                }
                if (state && !form.length) {
                    toastr.error('申请长度不能为空！');
                    state = false;
                }
                if (state && !form.xsdid && !form.xgmsid) {
                    toastr.error('发起方不能为空！');
                    state = false;
                }
                if (state && !form.ygmsid) {
                    toastr.error('接收方不能为空！');
                    state = false;
                }
                if (state && !form.note) {
                    toastr.error('说明不能为空！');
                    state = false;
                }
                if (state) {
                    return true;
                }  else {
                    setTimeout(function () {
                        that.find('button[type="submit"]').attr('disabled', false);
                    }, 500);
                    return false;
                }
            })
        });

        function getResidueLengthByRollNumberAndGmsId(roll_number, gmsid) {
            roll_number = $('[name="roll_number"]').val()
            gmsid = $('select[name="ygmsid"]').val()
            if (roll_number == '' || gmsid=='') {
                return false
            }
            request.get('/admin/ajax/getResidueLengthByRollNumberAndGmsId', {
                params: {
                    roll_number: roll_number,
                    gmsid: gmsid,
                }
            }).then( function(d) {
                var length = d.data
                if (length < 0) {
                    length = 0
                }
                $('.form-item').find('.residue-length b').text(length)
            })
        }

        function getGMSByRollNumber(roll_number) {
            roll_number = String(roll_number).trim()
            if (roll_number === '' || roll_number==='null' || roll_number===null) {
                $('select[name="ygmsid"]').empty()
                return false
            }
            request.get('/admin/ajax/getRGMS', {
                params: {
                    roll_number: roll_number
                }
            }).then( function(d) {
                var data = d.data
                $('select[name="ygmsid"]').val(' ')
                $('select[name="ygmsid"]').empty().append(makeSelectOptionsOnly(data));
                $('[name="ygmsid"]').trigger('change')
                getResidueLengthByRollNumberAndGmsId()
            })
        }

        $(document).ready(function () {
            $('body').on('focus', '.sq-roll', function () {
                $('[name="roll_number"]').parents('.form-item').find('.text-danger').addClass('d-none')
            })
            $('body').on('blur', '.sq-roll', function () {
                if ($('[name="ygmsid"]').val()) {
                } else {
                    $('[name="roll_number"]').parents('.form-item').find('.text-danger').removeClass('d-none')
                }
            });
            /*$('select[name="film_type_id"]').on('change', function () {
                $('select.sq-rolls').select2('val', ' ');
            })*/
            $('select[name="roll_number"]').on('change', function () {
                getGMSByRollNumber($(this).val())
            })
            $('select[name="ygmsid"]').on('change', function () {
                getResidueLengthByRollNumberAndGmsId()
            })
            /*不做删减*/
            $('input[name="length"]').on('change input propertychange', function () {
                var val = $(this).val()
                var max = $('.form-item').find('.residue-length b').text()
                val = Number(val)
                max = Number(max)
                if (val > max) {
                    val = max
                }
                if (val < 0) {
                    val = 0
                }
                $(this).val(val)
            })

            $('.sq-jxs').select2({
                ajax: {
                    type:'get',
                    url: '/admin/ajax/getJXSMC',
                    dataType: 'json',
                    delay: 250,dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            sdid: $('select[name="xsdid"]').val()
                        };
                    },
                    processResults: function (data) {
                        data = data['data']
                        items = []
                        for (var i = 0; i <= data.length - 1; i++) {
                            items[i] = {
                                id: data[i].id,
                                text: data[i].abbr
                            }
                        }
                        return {
                            results: items
                        };
                    },
                },
                selectOnClose: true
            });

            $('select.sq-rolls').select2({
                ajax: {
                    type:'get',
                    url: '/admin/ajax/getURRL',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            // film_type_id: $('select[name="film_type_id"]').val()
                        };
                    },
                    processResults: function (data) {
                        data = data['data']
                        items = []
                        for (var i = 0; i <= data.length - 1; i++) {
                            items[i] = {
                                id: data[i].roll_number,
                                text: data[i].roll_number
                            }
                            window.rolls_front_data[data[i].roll_number] = data[i]
                        }
                        return {
                            results: items
                        };
                    },
                },
                selectOnClose: true
            });
            $('[name="ygmsid"]').on('change', function () {
                getSdmc($(this).val()).then(function (d) {
                    $('[name="ysdmc"]').val(d.data)
                })
            });
            $('[name="xgmsid"]').on('change', function () {
                getSdmc($(this).val()).then(function (d) {
                    $('[name="xsdmc"]').val(d.data)
                })
            });
            // $('[name="xgmsid"]').trigger('change')
        });

        function getSdmc(id) {
            return new Promise(function (resolve, reject) {
                if (!id) {
                    return reject(null)
                }
                request.get('/admin/ajax/getSdmc', {
                    params: {
                        user_id: id
                    }
                })
                    .then(function (d) {
                        resolve(d)
                    })
                    .catch(function (d) {
                        reject(d)
                    })
            })
        }


        function getXsdmc() {

        }
        function getYsdmc() {

        }

    </script>

@endsection